﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public static class DataGridExtensions
    {
        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IEnumerable<T> dataSource) where T : class
        {
            return DataGrid(viewComponentFactory, dataSource, (IPersistentDataGridModel)null, null);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IEnumerable<T> dataSource, object htmlAttributes) where T : class
        {
            return DataGrid(viewComponentFactory, dataSource, (IPersistentDataGridModel)null, htmlAttributes);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IEnumerable<T> dataSource, IPersistentDataGridModel settings) where T : class
        {
            return DataGrid(viewComponentFactory, dataSource, settings, null);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IEnumerable<T> dataSource, IPersistentDataGridModel settings, object htmlAttributes) where T : class
        {
            return DataGrid(viewComponentFactory, new DataGridModel<T>() { DataResults = dataSource, InputModel = ((IDataGridInputModel)new DataGridInputModel(settings)) }, htmlAttributes);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IEnumerable<T> dataSource, IDataGridInputModel settings) where T : class
        {
            return DataGrid(viewComponentFactory, dataSource, settings, null);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IEnumerable<T> dataSource, IDataGridInputModel settings, object htmlAttributes) where T : class
        {
            return DataGrid(viewComponentFactory, new DataGridModel<T>() { DataResults = dataSource, InputModel = settings }, htmlAttributes);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IDataGridModel<T> model) where T : class
        {
            return DataGrid(viewComponentFactory, model, null);
        }

        public static DataGridBuilder<T> DataGrid<T>(this ViewComponentFactory viewComponentFactory, IDataGridModel<T> model, object htmlAttributes) where T : class
        {
            DataGrid<T> dataGrid = new DataGrid<T>(viewComponentFactory.Writer, model.DataResults, model.InputModel);

            dataGrid.Settings.TotalRecordCount = model.TotalNumberOfResults;

            if (htmlAttributes != null)
            {
                dataGrid.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new DataGridBuilder<T>(dataGrid);
        }
    }
}
